package cn.dyp.com.getnotification.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;


import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Base64;

import cn.dyp.com.getnotification.ClientApp;

/**
 * 网络连接工具类
 * 
 * @author asus
 */

public class NetUtil {
	private static Socket mClientSocket = null;
	private static BufferedReader mReader = null;
	private static String mNotifiStr = "";

	/**
	 * 判断网络是否连接
	 * 
	 * @param context
	 *            上下文对象
	 * @return 网络是否连接
	 */
	public static boolean isNetworkAvailable(Context context) {
		boolean isNetworkOK = false;
		try {
			ConnectivityManager conn = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (null == conn || null == conn.getActiveNetworkInfo()) {
				isNetworkOK = false;
			} else {
				isNetworkOK = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isNetworkOK;
	}

	/**
	 * 发送数据
	 * 
	 * @param point
	 *            端口号
	 * @param params
	 *            向服务器发送的数据
	 * @return 服务器返回的数据
	 */
	public static String sendData(int point, String params, ClientApp mApp) {
		String result = "";
		try {
			initConn(point, mApp);
			setParms();
			result = handleData(params, result);
			mNotifiStr = toNotifiString(result);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (mClientSocket != null) {
				try {
					mClientSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (mReader != null) {
				try {
					mReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return mNotifiStr;
	}

	/**
	 * 数据处理
	 * 
	 * @param params
	 *            发送的数据
	 * @param result
	 *            存储返回数据的字符串
	 * @return 服务器返回的数据
	 */
	private static String handleData(String params, String result) {
		try {
			mReader = new BufferedReader(new InputStreamReader(
					mClientSocket.getInputStream()));
			// 先向服务器端发送数据，以后回车为结束标记
			PrintWriter out = new PrintWriter(mClientSocket.getOutputStream());
			out.println(params);
			out.flush();

			// 再读取服务器端的回应数据，一行一行的读取
			String line;
			while ((line = mReader.readLine()) != null) {
				if (line.equals("")) {
					break;
				}
				result += line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 设置连接参数
	 * 
	 */
	private static void setParms() {
		try {
			mClientSocket.setSoTimeout(30 * 1000);
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化连接
	 * 
	 * @param point
	 *            端口号
	 * @param mApp
	 *            ClientApp
	 */
	private static void initConn(int point, ClientApp mApp) {
		try {
			mClientSocket = new Socket(mApp.getServerIpStr(), point);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 数据解析，Base64解码
	 * 
	 * @param result
	 *            加密的服务器返回字符串
	 */
	private static String toNotifiString(String result) {
		byte tmpB[] = Base64.decode(result, Base64.DEFAULT);
		mNotifiStr = new String(tmpB);
		return mNotifiStr;
	}
}
